<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>学段科研员评分</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=10,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" type="text/css" href="/lib/layui/layui/css/layui.css"/>
    <script type="text/javascript" src="/js/news/jquery-1.9.1.js"></script>
    <script src="/js/base/base.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../js/jquery/jquery.cookie.js"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.ui.widget.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.iframe-transport.js" type="text/javascript"
            charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.fileupload.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="/js/common/fileupload.js"></script>
    <script type="text/javascript" src="/lib/layui/layui/layui.js"></script>
    <script type="text/javascript" src="/lib/layui/layui/layui.all.js"></script>
</head>
<style>
    .mbox {
        padding: 8px
    }
    .item img {
        height: 40px;
    }
    .btn {
        height: 37px;
    }
    .btn button {
        float: right;
    }
    .openFile input[type=file] {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 18px;
        z-index: 99;
        opacity: 0;
    }
    .typeIpt {
        line-height: 30px;
        margin-left: 10px;
    }
    .layui-form-label {
        width: 100px;
    }
</style>
<body>
<div class="mbox">
    <div class="item">
        <img src="../img/yeji.png" alt="" style="margin: 0 5px 0 20px;">
        <span style="font-size: 22px;display: inline-block;vertical-align: middle;">学段科研员评分</span>
    </div>
    <hr class="layui-bg-blue">
    <div class="btn" style="display: inline-flex;">
        <span class="typeIpt layui-form">
            <input type="radio" checked class="approve" lay-filter="status" value="1" name="status" title="未上传"/>&nbsp;
            <input type="radio" lay-filter="status" value="2" name="status" title="已上传"/>&nbsp;
            <input type="radio" lay-filter="status" value="3" name="status" title="已提交"/>&nbsp;
<%--            <input type="radio" lay-filter="status" value="4" name="status" title="未批准"/>&nbsp;--%>
        </span>
    </div>
    <div class="btn">
        <button type="button" class="layui-btn layui-btn-sm" style="background: #2b7fe0">
            <i class="layui-icon layui-icon-upload-drag" style="background: #2b7fe0"></i>导出Excel
        </button>
        <button id="subBtn" type="button" class="layui-btn layui-btn-sm" style="background: #2b7fe0;margin-right: 20px; display: none">
            <i class="layui-icon layui-icon-addition"  style="vertical-align: inherit"></i>提交
        </button>
    </div>
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
</body>
</html>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="score">上传评分</a>
</script>
<script>
    var noDataTable;

    layui.use(['table', 'form', 'layer', 'laydate', 'upload', ], function () {
        var table = layui.table,
            form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate,
            upload = layui.upload;

        form.render();

        // // 初始化结题结果
        // $.ajax({
        //     url: '/conclusion_apply/initializationConclusionData'
        //     , type: 'post'
        //     , data: {
        //         trType: "24"
        //         , trStatus: "1"
        //         , type: "23"
        //         , status: "0"
        //         , flag: false
        //     }
        //     , dataType: 'json'
        //     , async: false
        //     , success: function (res) {}
        // });

        // 未上传/已上传表格数据
        var tableData = table.render({
            elem: '#test'
            , url: '/conclusion_review_result/getReviewResult'
            , where: {
                trType: 24
                , pageType: 1
            }
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , defaultToolbar: ['']
            , title: '用户数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'topicName', title: '项目名称'}
                , {field: 'planName', title: '申报计划名称'}
                // , {field: 'reviewFraction', title: '专家平均分'}
                , {field: 'trOther2', title: '学段评分'}
                , {field: 'names', title: '项目成员'}
                , {field: 'headUserName', title: '负责人'}
                , {field: 'type', title: '操作', toolbar: '#barDemo'}
            ]]
            , page: true
        });

        // 页签转换
        form.on('radio(status)', function (data) {
            switch (data.value) {
                case "1":   // 未上传数据
                    $("#subBtn").css("display", "none");
                    tableData.reload({
                        where: {
                            trType: 24
                            , pageType: 1
                        }
                    });
                    break;
                case "2":   // 已上传数据
                    $("#subBtn").css("display", "block");
                    tableData.reload({
                        where: {
                            trType: 24
                            , pageType: 2
                        }
                    });
                    break;
                case "3":   // 批准
                    $("#subBtn").css("display", "none");
                    noBtnTable(24, 2, 3);
                    break;
                // case "4":   // 未批
                //     $("#subBtn").css("display", "block");
                //     tableData.reload({
                //         where: {
                //             trType: 24
                //             , trStatus: 3
                //             , pageType: 4
                //         }
                //     });
                //     break;
            }
        });

        // 无工具栏表格
        function noBtnTable(trType, trStatus, pageType) {
            noDataTable = table.render({
                elem: '#test'
                , url: '/conclusion_review_result/getReviewResult'
                , where: {
                    trType: trType
                    , trStatus: trStatus
                    , pageType: pageType
                }
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    , layEvent: 'LAYTABLE_TIPS'
                    , icon: 'layui-icon-tips'
                }]
                , defaultToolbar: ['']
                , title: '用户数据表'
                , cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    , {field: 'topicName', title: '项目名称'}
                    , {field: 'planName', title: '申报计划名称'}
                    // , {field: 'reviewFraction', title: '专家平均分'}
                    , {field: 'trOther2', title: '学段评分'}
                    , {field: 'names', title: '项目成员'}
                    , {field: 'headUserName', title: '负责人'}
                ]]
                , page: true
            });
        }

        // 监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            console.log(data)
            if(obj.event === 'score'){
                layer.open({
                    type: 1,
                    title: '上传评分',
                    btn: ['确定', '取消'],
                    shade: 0.5,
                    area: ['50%', '70%'],
                    content: '<div id="cont" style="margin: 10px">\n' +
                        '            <div class="layui-form-item" style="margin-top: 15px">\n' +
                        '                <div class="layui-inline">\n' +
                        '                   <span style="color:red">*</span><label class="layui-form-label">项目名称:</label>\n' +
                        '                    <div class="layui-input-inline">\n' +
                        '                        <input type="text" name="planName" disabled lay-verify="required|phone" value="'+empty(data.topicName)+'" autocomplete="off"\n' +
                        '                               class="layui-input required">\n' +
                        '                    </div>\n' +
                        '                </div>\n' +
                        '            </div>\n' +
                        '            <div class="layui-form-item" style="margin-top: 15px">\n' +
                        '                <div class="layui-inline">\n' +
                        '                   <span style="color:red">*</span><label class="layui-form-label">申报计划:</label>\n' +
                        '                    <div class="layui-input-inline">\n' +
                        '                        <input type="text" name="planName" disabled lay-verify="required|phone" autocomplete="off"\n' +
                        '                               class="layui-input required" value="'+empty(data.planName)+'">\n' +
                        '                    </div>\n' +
                        '                </div>\n' +
                        '            </div>\n' +
                        ' <div class="layui-inline share">\n' +
                        '                    <label class="layui-form-label">负责人:</label>\n' +
                        '                    <div class="layui-input-inline">\n' +
                        '                        <input type="text" id="rescueUser" user_id="" value="'+empty(data.headUserName)+'" lay-verify="url" autocomplete="off" class="layui-input" disabled style="width: 192px">\n' +
                        '                    </div>\n' +
                        '                </div>'+
                        ' <div class="layui-form-item"  style="margin-top:15px">\n' +
                        '    <label class="layui-form-label" style="width: 100px;margin-left:-20px">学段意见</label>\n' +
                        '    <div class="layui-input-inline">\n' +
                        '<div id="fujians"></div>' +
                        ' <div id="fileAll">\n' +
                        '</div>\n' +
                        '<a href="javascript:;" class="openFile" style="float: left;margin-top:8px;position:relative">\n' +
                        '<img src="../img/mg11.png" alt="">\n' +
                        '<span>添加附件</span>\n' +
                        '<input type="file" multiple id="fileupload" data-url="/upload?module=conclusion" name="file">\n' +
                        '</a>\n' +
                        '</div>\n' +
                        '</div>' +
                        '            <div class="layui-form-item" style="margin-top: 15px">\n' +
                        '                <div class="layui-inline">\n' +
                        '                   <span style="color:red">*</span><label class="layui-form-label">学段评分:</label>\n' +
                        '                    <div class="layui-input-inline">\n' +
                        '                        <input type="text" name="trOther2" lay-verify="required|phone" autocomplete="off"\n' +
                        '                               class="layui-input required"  value="'+empty(data.trOther2)+'">\n' +
                        '                    </div>\n' +
                        '                </div>\n' +
                        '            </div>\n' +
                        // '            <div class="layui-form-item" style="margin-top: 15px">\n' +
                        // '                <div class="layui-inline">\n' +
                        // '                   <span style="color:red">*</span><label class="layui-form-label">专家平均分:</label>\n' +
                        // '                    <div class="layui-input-inline">\n' +
                        // '                        <input type="text" name="planName" disabled lay-verify="required|phone" autocomplete="off"\n' +
                        // '                               class="layui-input required" value="'+empty(data.reviewFraction)+'">\n' +
                        // '                    </div>\n' +
                        // '                </div>\n' +
                        // '            </div>\n'+
                        '        </form>\n' +
                        '    </div>',
                    success: function () {
                        //附件上传
                        form.render();
                        fileuploadFn('#fileupload', $('#fileAll'));
                    },
                    yes:function (index) {
                        var attachmentId = '';
                        var attachmentName = '';
                        for (var i = 0; i < $('#fileAll .dech').length; i++) {
                            attachmentId += $('#fileAll .dech').eq(i).find('input').val();
                            attachmentName += $('#fileAll a').eq(i).attr('name');
                        }
                        $.ajax({
                            url: '/conclusion_review_result/uploadScore',
                            dataType: 'json',
                            type: 'get',
                            data: {
                                trOther3:attachmentId,
                                trOther4:attachmentName,
                                trOther2:$('input[name="trOther2"]').val(),
                                trId:data.trId
                            },
                            success: function (res) {
                                if (res.code == 0) {
                                    // 获取当前页签的值
                                    var pageType = $('input[name="status"]:checked').val()
                                    layer.msg("上传成功", {time: 1500, icon: 1});
                                    if (pageType == 1) {
                                        tableData.reload({
                                            where: {
                                                trType: 24
                                                , pageType: 1
                                            }
                                        });
                                    }else if (pageType == 2) {
                                        tableData.reload({
                                            where: {
                                                trType: 24
                                                , pageType: 2
                                            }
                                        });
                                    }
                                    // else if (pageType == 4) {
                                    //     // 刷新表格
                                    //     tableData.reload({
                                    //         where: {
                                    //             trType: 24
                                    //             , trStatus: 3
                                    //             , pageType: 4
                                    //         }
                                    //     });
                                    // }
                                } else {
                                    layer.msg("上传失败", {time: 1500, icon: 2});
                                }
                                layer.close(index);
                            }
                        });
                    }
                });
            }
        });

        // 课题组上传意见点击事件
        $("#subBtn").click(function () {
            // 获取当前页签的值
            var pageType = $('input[name="status"]:checked').val()
            // 获取复选框的值
            var checkData = layui.table.checkStatus('test').data;
            if (checkData.length > 0) {
                var trIds = "";
                var topicIds = ""
                for (var i = 0; i < checkData.length; i++) {
                    trIds += checkData[i].trId + ",";
                    topicIds += checkData[i].topicId + ",";
                }
                $.ajax({
                    url: '/conclusion_apply/submitData'
                    , type: 'post'
                    , data: {
                        ids: trIds
                        , newTrType: 23
                        , newTrStatus: 1
                        , oldTrType: 24
                        , oldTrStatus: 1
                        , oldStatusChange: 2
                    }
                    , dataType: 'json'
                    , async: false
                    , success: function (res) {
                        var msg = "";
                        if (res.code == 0) {
                            layer.msg("提交成功", {time: 1500, icon: 1});
                        } else {
                            layer.msg("提交失败", {time: 1500, icon: 2});
                        }
                        tableData.reload({
                            where: {
                                trType: 24
                                , pageType: 2
                            }
                        });
                    }
                });
                // subResultsData(trIds, 2, 25, 1, pageType)
            } else {
                layer.msg("请选择需要提交至课题组的数据", {time: 1500, icon: 7});
            }
        });

        // 提交至课题组通用方法
        function subResultsData(trIds, oldTrStatus, newTrType, newTrStatus, pageType) {
            // 提交给课题组
            $.ajax({
                url: '/conclusion_review_result/subResultsData'
                , type: 'post'
                , data: {
                    trIds: trIds
                    , oldTrStatus: oldTrStatus
                    , newTrType: newTrType
                    , newTrStatus: newTrStatus
                }
                , dataType: 'json'
                , success: function (res) {
                    if (res.code == 0) {
                        layer.msg("提交课题组成功", {time: 1500, icon: 1});
                        if (pageType == 2) {
                            tableData.reload({
                                where: {
                                    trType: 24
                                    , pageType: 2
                                }
                            });
                        } else if (pageType == 4) {
                            // 刷新表格
                            tableData.reload({
                                where: {
                                    trType: 24
                                    , trStatus: 3
                                    , pageType: 4
                                }
                            });
                        }
                    } else {
                        layer.msg("提交课题组失败", {time: 1500, icon: 2});
                    }
                }
            });
        }

        //判断空的方法
        function empty(name){
            if(name==undefined||name==''){
                return ''
            }else{
                return name
            }
        }

        //删除附件
        $(document).on('click', '.deImgs', function () {
            var _this = this;
            var attUrl = $(this).parents('.dech').attr('deUrl');
            layer.confirm('确定删除该附件吗？', function (index) {
                $.ajax({
                    type: 'get',
                    url: '/delete?' + attUrl,
                    dataType: 'json',
                    success: function (res) {
                        if (res.flag == true) {
                            layer.msg('删除成功', {icon: 6, time: 1000});
                            $(_this).parent().remove();
                        } else {
                            layer.msg('删除失败', {icon: 2, time: 1000});
                        }
                    }
                })
            });
        });
    });
</script>
